package com.princess.demok.mvvm.view

import android.view.View
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.princess.demok.R
import com.princess.demok.base.MyApplication
import com.princess.demok.base.view.BaseLifeCycleActivity
import com.princess.demok.common.State
import com.princess.demok.common.StateType
import com.princess.demok.mvvm.viewmodel.HistoryListViewModel
import com.princess.demok.net.data.HistoryData
import com.princess.demok.ui.adapter.HistoryListAdapter
import com.princess.demok.ui.home.HisDetailActivity
import kotlinx.android.synthetic.main.layout_recycler_just.*

/**
 * @author: xha
 * @date: 2021/6/7 15:24
 * @Description:
 */
class HistoryListMvvmActivity : BaseLifeCycleActivity<HistoryListViewModel>() {

    private val historyAdapter by lazy {
        HistoryListAdapter()
    }

    override fun initDataObserver() {
        mViewModel.historyData.observe(this, {
            it?.let {
                historyAdapter.setList(it)
                MyApplication.mContext.hisList = it
            }
        })
    }

    override fun getLayoutId(): Int? {
        return R.layout.activity_recycler_just
    }

    override fun initData() {
        setTitleText("历史上的今天")
        setAdapter()
        val hisList = MyApplication.mContext.hisList
        if (hisList.isNotEmpty()) {
            historyAdapter.setList(hisList)
            mViewModel.loadState.postValue(State(StateType.SUCCESS))
        } else {
            mViewModel.getData()
        }

        val adapter1: ConcatAdapter = ConcatAdapter()
    }

    private fun setAdapter() {
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = historyAdapter

        val viewEmpty = View.inflate(this, R.layout.layout_nodata, null)
        historyAdapter.setEmptyView(viewEmpty)

        historyAdapter.setOnItemClickListener { adapter, view, position ->
            val hisData = adapter.getItem(position) as HistoryData
            HisDetailActivity.start(this, hisData)
        }

    }

}